#include <myhead.h>
#include "page.h"
#include "client.h"

void* page_to_friend_index(){
	print_nav2("首页/聊天功能", (char*)client_loginname());
	printf("[指令提示]:\n");
	printf("\t1-好友列表\n");
	printf("\t2-好友聊天\n");
	printf("\t3-添加好友\n");
	printf("\t4-删除好友\n");
	printf("\tP-返回上一页\n");

	char ch= getInputChar(NULL);
	switch(ch){
	case '1':
		return page_to_friend_list;
	case '2':
		return page_to_friend_chat;
	case '3':
		return page_to_friend_add;
	case '4':
		return page_to_friend_delete;
	case 'P':
		return page_to_index;
	}
	fprintf(stderr, "输入的指令错误\n");
	return page_to_friend_index;
}

void* page_to_friend_list(pkg_t* retPkg, char* errmsg){
	print_nav2("首页/聊天功能/好友列表", (char*)client_loginname());
	//打包登录协议报
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_FRD_LIST,1, (char*)client_loginname());
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_friend_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("好友列表查询失败,返回", pkg_get(retPkg, 1));
		return page_to_friend_index;
	}
	//
	print_green("您的好友列表");
	printf("%s\n", pkg_get(retPkg, 1));
	return page_to_friend_index;
}


void* page_to_friend_chat(pkg_t* retPkg, char* errmsg){
	print_nav2("首页/聊天功能/好友聊天", (char*)client_loginname());
	printf("[输入好友名称]:");
	char friend[30]={0};
	fgets(friend, 30, stdin);
	friend[strlen(friend)-1]=0;
	//
	printf("[输入消息]:");
	char msg[512]={0};
	fgets(msg, 512, stdin);
	msg[strlen(msg)-1]=0;
	//打包
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_FRD_CHAT,3,(char*)client_loginname(),friend, msg);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_friend_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("发送失败", pkg_get(retPkg, 1));
		return page_to_friend_index;
	}
	print_green("成功发送消息");
	return page_to_friend_index;
}

void* page_to_friend_add(pkg_t* retPkg, char* errmsg){
	print_nav2("/首页/聊天功能/添加好友", (char*)client_loginname());
	printf("[输入好友名称]:");
	char friend[30]={0};
	fgets(friend, 30, stdin);
	friend[strlen(friend)-1]=0;
	//打包
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_FRD_ADD,2,(char*)client_loginname(),friend);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);
	if(ret != 0){
		print_red2("通信异常", errmsg);
		return page_to_friend_index;
	}
	if(strcmp(pkg_get(retPkg, 0), "OK") != 0){
		print_red2("添加失败", pkg_get(retPkg, 1));
		return page_to_friend_index;
	}
	print_green("好友添加成功");
	return page_to_friend_index;
}


void* page_to_friend_delete(pkg_t* retPkg, char* errmsg){
	print_nav2("/首页/聊天功能/删除好友", (char*)client_loginname());
	print_red("暂未实现，来不及写了");
	return page_to_friend_index;
}

